package com.neusoft.neuiotms.dcom.repository.insurance.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceTaskStatus;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceRegisterTask;
import com.neusoft.neuiotms.dcom.model.insurance.QInsuranceRegisterTask;
import com.neusoft.neuiotms.dcom.repository.insurance.CustomizedInsuranceRegisterTaskRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInsuranceRegisterTaskRepositoryImpl extends AbstractRepository
        implements CustomizedInsuranceRegisterTaskRepository {

    @Override
    public List<InsuranceRegisterTask> index(final Searchable searchable) {
        final QInsuranceRegisterTask qInsuranceRegisterTask = QInsuranceRegisterTask.insuranceRegisterTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InsuranceRegisterTask> query = this.query().selectFrom(qInsuranceRegisterTask);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<InsuranceRegisterTask> search(final Pageable pageable, final Searchable searchable) {
        final QInsuranceRegisterTask qInsuranceRegisterTask = QInsuranceRegisterTask.insuranceRegisterTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InsuranceRegisterTask> query = this.query().selectFrom(qInsuranceRegisterTask);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return InsuranceRegisterTask.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QInsuranceRegisterTask qInsuranceRegisterTask = QInsuranceRegisterTask.insuranceRegisterTask;

        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            // 编码
            where.and(super.equalsStr(qInsuranceRegisterTask.code, searchable, "code"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("unitCode"))) {
            // 单位
            where.and(super.equalsStr(qInsuranceRegisterTask.unitCode, searchable, "unitCode"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("registerRemarks"))) {
            // 备注
            where.and(super.containsStr(qInsuranceRegisterTask.registerRemarks, searchable, "registerRemarks"));
        }

        if (null != searchable.getStrArray("unitCodes")) {
            // 单位集
            where.and(super.inStrings(qInsuranceRegisterTask.unitCode, searchable, "unitCodes"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            // 状态检索
            where.and(super.equalsEnum(qInsuranceRegisterTask.status, searchable, "status",
                    value -> InsuranceTaskStatus.valueOf(value)));
        }

        return where;
    }

}
